<?php

/*
 * anyone can send message to user that show in personalMessage
 * and open the template in the editor.
 */

/**
 * Description of BOpCnPersonalMessage
 *
 * @author I062261
 */
class BOCServiceMessage extends BOperationContract {

    const Operation_SendMessage = 'sendMessage';
    //const User_Type_ServiceAllUser = -65201001;
    const Receiver_Scope_AllServicesOfCurrentUser = "allServicesOfUser";
    const Receiver_Scope_ServiceOfAllUsers = "serviceOfAllUsers";

    protected function registerOperationContext($operation = NULL, $receiver = NULL, $sender = NULL) {
        //$personalServiceId = Blyn::app()->getCurrentUser()->getPersonalService()->getId();
        //$serviceAppId = Blyn::app()->getCurrentUser()->getPersonalService()->getServiceEngine()->getId();

        if ($operation == NULL && $this->operationName == NULL)
            $this->operationName = self::Operation_SendMessage;

        if ($this->operationName == self::Operation_SendMessage) {

            //register ServiceApp
            $serviceEngineId = Blyn::app()->getActiveService()->getServiceEngine()->getId();

            $this->operations[self::Operation_SendMessage]['receiverContext'] = array(
                self::ReceiverContext_ServiceApp => self::Context_Value_FixedValue,
                'data' => array('serviceAppId' => $serviceEngineId)
            );

            $currentUser = Blyn::app()->getCurrentUser();
            $activeService = Blyn::app()->getActiveService();

            if ($currentUser->getLoginId() == 'bAdmin@bilyn.com'
                    && $activeService->getName() == "bilynAdmin"
                    && $receiver == self::User_Scope_AllUser) {
                $this->operations[$this->operationName]['receiverContext'][self::ReceiverContext_User] = self::User_Scope_AllUser;
                $this->operations[$this->operationName]['receiverContext'][self::ReceiverContext_Service] = self::Service_Scope_AllService;
            }

            //send message to current user's of specific teamtype
            //send to specific serviceteam message, basically can only send message within sender's service           
            $basicRolesTemplate = new BBasicRolesTemplate();
            $basicRoles = $basicRolesTemplate->getAllBasicRoles();

            //specify receiver as one of team
            if (in_array($receiver, $basicRoles)) {
                $newReceiver['serviceTeam'] = $receiver;
                $receiver = $newReceiver;
            }

            //send message to serviceTeam
            if (is_array($receiver) && isset($receiver['serviceTeam']) && $receiver['serviceTeam'] != NULL) {
                $this->operations[$this->operationName]['receiverContext'][self::ReceiverContext_User] = self::Context_Value_UserType_Role;
                $roleId = Blyn::app()->getActiveServiceApp()->getAuthManager()->getRoleId($receiver['serviceTeam'], $serviceEngineId);
                $this->operations[$this->operationName]['receiverContext']['data']['roleId'] = $roleId;
            }

            //if not set receiver's serviceId, message send to current service
            if (!isset($receiver['serviceId'])) {
                $this->operations[$this->operationName]['receiverContext'][self::ReceiverContext_Service] = self::Context_Value_FixedValue;
                $this->operations[$this->operationName]['receiverContext']['data']['serviceId'] = Blyn::app()->getActiveService()->getId();
            }

            if (isset($receiver['serviceId'])) {
                $this->operations[$this->operationName]['receiverContext'][self::ReceiverContext_Service] = self::Context_Value_RuntimeValue;
            }

            if (isset($receiver['userId']) && $receiver['userId'] > 0) {
                $this->operations[$this->operationName]['receiverContext'][self::ReceiverContext_User] = self::Context_Value_RuntimeValue;
            }
        }
    }

    public function sendOperationRequest($operation, $receiver = NULL, $sender = NULL) {

        parent::sendOperationRequest($operation, $receiver, $sender);
    }

    protected function addOperationRequestToStore($operation, $status) {
        if ($this->checkSenderPermit($operation, $operation['sender'], $operation['receiver'])) {
            if ($operation['receiver'] != NULL) {
                Blyn::app()->getAppDb()->addMessageRequest(array(
                    'receiver' => $operation['receiver'],
                    'sender' => $operation['sender'],
                    'operation' => array('name' => $operation['name']),
                    //     'status' => array('status' => 'pending', 'startTime' => time(), 'aliveLength' => 7 * 24 * 60 * 60), //alive of request is one week
                    'status' => $status,
                    'data' => $operation['data']
                ));
            }
        }
    }

    protected function getStoredOperationRequest($operationName, $userList, $serviceList, $serviceAppList, $status) {

        $db = Blyn::app()->getActiveServiceApp()->getDbAdapter();

        $opRequests = array();

        foreach ($userList as $userData) {
            foreach ($serviceList as $serviceData) {
                foreach ($serviceAppList as $appData) {
                    $oRequests = $db->getMessageRequests($operationName, $userData, $serviceData, $appData, $status);
                    foreach ($oRequests as $oRequest) {
                        $opRequests[$oRequest->id] = $oRequest;
                    }
                    //$opRequests = array_merge($opRequests, $db->getOperationRequests($userData, $serviceData, $appData, array('status' => $status, 'timeLength' => $timeLength)));
                }
            }
        }


        //resort array from latest to before
        krsort($opRequests);

        return $opRequests;
    }

}

?>
